-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Time singleton #49123
Add a Time singleton #49123
Conversation
I think should a Date, a Time and a DateTime class. like Qt This classes very useful for me. |
@CsloudX Definitely worth considering, as that's kinda what JS and Qt do. I don't think we need 3 of them though. However, for comparison purposes, you can just use the Unix timestamp which is an |
Timezones are hard, so we should probably let the OS handle that if at all possible. Otherwise, I would let the user handle it -- or alternatively, don't handle it at all. |
Haha, I knew that was the Tom Scott video before clicking it. I don't love how this uses so many massive macros. They could all easily be converted to methods which return a struct with |
@EricEzaM That would indeed make sense for some of them like |
I added workaround to CI, so it should pass now without any negative impact of this changes. |
I think there should be a |
I think it's better to leave this to a separate PR (and proposal). Otherwise, we risk making this PR huge and rejected in the end. |
1c792ed
to
91a68f8
Compare
Thanks! |
Looking at the function |
This was likely done to prevent an overflow if you have a Godot process that runs for more than ~49.7 days (which is possible with a dedicated server). |
Note: This PR will fail the CI checks because @qarmin's test project uses the methods fromOS
. There is nothing that I can do about this, the test project will simply have to be updated after this PR is merged.Implements and closes #37800, implements and closes godotengine/godot-proposals#2114.
This PR adds a new
Time
singleton for working with time. Some of these methods are moved out of_OS
incore_bind
(which is the same as the exposedOS
but is not the same as the internalOS
), and some are new methods (including those requested by proposal 2114).Compared to the current master, this code:
p_
now, etc.There are many new methods added by this PR to suit many use cases. They handle 3 formats: the Unix timestamp, a
datetime
dictionary, and ISO 8601 date and time strings (without timezone, as it's not stored in the Unix timestamp). Not all are strictly necessary as they can be built from the others, but not nearly as efficiently (going from dictionary -> Unix timestamp -> string and vice versa requires calculating the Unix timestamp, going from Unix timestamp -> dictionary -> string and vice versa requires constructing a dictionary, and going from Unix timestamp -> string -> dictionary and vice versa requires constructing and decomposing a string and doing extra calculations to find the weekday if that's desired). Some can be removed if desired, but I suggest keeping them all as there is very little code duplication - the heavy lifting is abstracted to macros at the top oftime.cpp
.In addition to converting time, several of the methods are for getting time information from the operating system, in which case they call
OS
- the internal one, the previously exposed methods were in_OS
. Remember that these methods were already "duplicated" before (_OS
and internalOS
), they've just moved (so it's nowTime
and internalOS
).Open question: Timezones and DST are not handled. Should they be? It would add complication to the code and require the user to supply more information: what the timezone is, whether DST is active (can't have an "auto" because different countries have DST at different times, except it is possible only when getting the current time from the OS but this part is already handled), and whether the time should be converted back/forth/or not. It might be simpler for the end user to require users to manually add/subtract/append/trim the timezone before/after converting as necessary.
Open question: Every single method starts with
get_
. It might make sense to just exclude this prefix.Testing and feedback would be appreciated.